/*
 * Copyright (C) 2011 Dirk Weißenborn, dirk.weissenborn@gmail.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.weissenborn.colabtex.client.activities;

import com.weissenborn.colabtex.client.ClientFactory;
import com.weissenborn.colabtex.client.places.Dashboard;
import com.weissenborn.colabtex.client.places.Login;
import com.weissenborn.colabtex.client.service.UserManagementService;
import com.weissenborn.colabtex.client.service.UserManagementServiceAsync;
import com.weissenborn.colabtex.client.view.LoginView;

import com.google.gwt.activity.shared.AbstractActivity;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.place.shared.Place;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AcceptsOneWidget;


/**
 * The Class LoginActivity  which controls the view of the login process
 */
public class LoginActivity extends AbstractActivity implements LoginView.Presenter {
	
	/** The client factory. */
	private final ClientFactory clientFactory;
	
	/** The place. */
	private Login place;

	/**
	 * Instantiates a new login activity.
	 *
	 * @param place the loginplace which contains information about the next place to go to
	 * @param clientFactory the client factory
	 */
	public LoginActivity(Login place, ClientFactory clientFactory) {
		this.clientFactory = clientFactory;
		if(place.getDesiredPlace() instanceof Login)
			place.setDesiredPlace(new Dashboard());
		
		this.place = place;
		
		if(clientFactory.getSessionID()!=null)
			login(clientFactory.getSessionID());
		if(clientFactory.isLoggedIn())
			goTo(place.getDesiredPlace());
	}

	/* (non-Javadoc)
	 * @see com.google.gwt.activity.shared.Activity#start(com.google.gwt.user.client.ui.AcceptsOneWidget, com.google.gwt.event.shared.EventBus)
	 */
	public void start(AcceptsOneWidget containerWidget, EventBus eventBus) {
		final LoginView view = clientFactory.getLoginView();		
		view.setPresenter(this);		
		
		containerWidget.setWidget(view.asWidget());
	}

	/* (non-Javadoc)
	 * @see com.weissenborn.colabtex.client.view.LoginView.Presenter#goTo(com.google.gwt.place.shared.Place)
	 */
	public void goTo(Place place) {
		clientFactory.getPlaceController().goTo(place);
	}
	
	/* (non-Javadoc)
	 * @see com.weissenborn.colabtex.client.view.LoginView.Presenter#login(java.lang.String, java.lang.String)
	 */
	public void login(final String username, String password) {
		UserManagementServiceAsync service = UserManagementService.Util.getInstance();

		AsyncCallback<String> callback = new AsyncCallback<String>() {

			public void onFailure(Throwable caught) {
				Window.alert("No connection to server!");
			}

			public void onSuccess(String result) {
				if(result != null) {
					clientFactory.setSessionID((String)result);				
					clientFactory.setLoggedIn(true);
					clientFactory.setUser(username);
					goTo(place.getDesiredPlace());
				}	
				else {
					Window.alert("Wrong username or password!");
				}
			}
		};
		
		service.login(username, password, callback);
	}

	/**
	 * Login.
	 *
	 * @param token the token or session id which is saved by the browser after the first login
	 */
	protected void login(String token) {
		UserManagementServiceAsync service = UserManagementService.Util.getInstance();

		AsyncCallback<String> callback = new AsyncCallback<String>() {

			public void onFailure(Throwable caught) {
				Window.alert("No connection to server!");
			}

			public void onSuccess(String result) {
				if(result == null) {
					clientFactory.setSessionID(null);					
				}
				else {	
					clientFactory.setLoggedIn(true);
					clientFactory.setUser(result);
					goTo(place.getDesiredPlace());
				}
			}
		};
		
		service.getUser(token, callback);
	}
	
	/* (non-Javadoc)
	 * @see com.weissenborn.colabtex.client.view.LoginView.Presenter#register(java.lang.String, java.lang.String)
	 */
	public void register(String username, String password) {
		UserManagementServiceAsync service = UserManagementService.Util.getInstance();

		AsyncCallback<Boolean> callback = new AsyncCallback<Boolean>() {

			public void onFailure(Throwable caught) {
				Window.alert("No connection to server!");
			}

			public void onSuccess(Boolean result) {
				if(result) {
					Window.alert("You should receive a mail with your login password! It will expire after 1 hour, if you do not sign in before that!\n" +
							"If you cannot find it, have a look in your spam!");
				}
				else {	
					Window.alert("Couldn't register your email!");
				}
			}
		};
		
		service.register(username, callback);
	}

	/* (non-Javadoc)
	 * @see com.weissenborn.colabtex.client.view.LoginView.Presenter#forgotPassword(java.lang.String)
	 */
	public void forgotPassword(String username) {
		if(Window.confirm("An email with a new password will be sent to your mail adress.\nYou really want to reset your password?"))
		{		
			UserManagementServiceAsync service = UserManagementService.Util.getInstance();
			
			AsyncCallback<Boolean> callback = new AsyncCallback<Boolean>() {

				public void onFailure(Throwable caught) {
					Window.alert("No connection to server!");
				}

				public void onSuccess(Boolean result) {
					if(result) {
						Window.alert("You should receive a mail with your login password! It will expire after 1 hour, if you do not sign in before that!");
					}
					else {	
						Window.alert("Couldn't reset your password, maybe the email you are using is wrong!");
					}
				}
			};
			
			service.resetPassword(username, callback);
		}
	}
}
